package com.allways.dubbo.mdm.service.logiareadetail.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.logiareadetail.Logiareadetail;
import com.allways.dubbo.mdm.service.logiareadetail.ILogiareadetailService;

/**
 * <p>ClassName: ILogiareadetailService</p>
 * <p>Description: 物流运作区域详情Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-09</p>
 */
@Service("mdmLogiareadetailService")
public class LogiareadetailServiceImpl extends MdmBaseDaoImpl implements ILogiareadetailService {
	Log log = LogFactory.getLog(LogiareadetailServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addLogiareadetail(Logiareadetail logiareadetail) {
   		log.debug("For the new logiareadetail parameters:"+ logiareadetail.toJson());
   		validColumnLen(logiareadetail);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(logiareadetail.getLogiareadetailCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getLogiareadetailByCode(logiareadetail.getLogiareadetailCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("logiareadetail.insertLogiareadetail",logiareadetail);
    }

    @Override
    public int updateLogiareadetail(Logiareadetail logiareadetail) {
    	log.debug("Update for the logiareadetail parameters:"+ logiareadetail.toJson());
    	validColumnLen(logiareadetail);
     	//无code的不能更新
        if(StringUtils.isEmpty(logiareadetail.getLogiareadetailCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Logiareadetail oldLogiareadetail = getLogiareadetailByCode(logiareadetail.getLogiareadetailCode());
        if(null == oldLogiareadetail){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldLogiareadetail.setDetailId(logiareadetail.getDetailId());
		oldLogiareadetail.setLaId(logiareadetail.getLaId());
		oldLogiareadetail.setLaCode(logiareadetail.getLaCode());
		oldLogiareadetail.setPointId(logiareadetail.getPointId());
		oldLogiareadetail.setPointCode(logiareadetail.getPointCode());
		oldLogiareadetail.setNationCode(logiareadetail.getNationCode());
		oldLogiareadetail.setProvinceCode(logiareadetail.getProvinceCode());
		oldLogiareadetail.setCityCode(logiareadetail.getCityCode());
		oldLogiareadetail.setDistrictCode(logiareadetail.getDistrictCode());
		oldLogiareadetail.setDisplay(logiareadetail.getDisplay());
		oldLogiareadetail.setDeleteMark(logiareadetail.getDeleteMark());
		oldLogiareadetail.setDataSource(logiareadetail.getDataSource());
		oldLogiareadetail.setCreateUser(logiareadetail.getCreateUser());
		oldLogiareadetail.setCreateTime(logiareadetail.getCreateTime());
		oldLogiareadetail.setUpdateUser(logiareadetail.getUpdateUser());
		oldLogiareadetail.setUpdateTime(logiareadetail.getUpdateTime());
        
        return update("logiareadetail.updateLogiareadetail",oldLogiareadetail);
    }

    @Override
    public int deleteLogiareadetail(Logiareadetail logiareadetail) {
    	log.debug("Delete for the logiareadetail parameters:"+ logiareadetail.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(logiareadetail.getLogiareadetailCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("logiareadetail.deleteLogiareadetail",logiareadetail);
    }

    @Override
    public Logiareadetail getLogiareadetailByCode(String code) {
    	log.debug("Query for logiareadetail code:" + code);
        return queryForObject("logiareadetail.getLogiareadetailByCode",code);
    }
    @Override
    public long getLogiareadetailCount(Logiareadetail logiareadetail) {
    	log.debug("Query for logiareadetail count parameters:" + logiareadetail.toJson());
        return queryForObject("logiareadetail.getLogiareadetailCount",logiareadetail);
    }
   @Override
    public List<Logiareadetail> queryLogiareadetails(Logiareadetail logiareadetail, int firstRow, int lastRow) {
      	log.debug("Query for logiareadetail parameters:" + logiareadetail.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("logiareadetail",logiareadetail);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("logiareadetail.queryLogiareadetails", param);
    }
    
    /**
     * 验证字段长度
     * @param logiareadetail 需要验证的对象
     */
    private void validColumnLen(Logiareadetail logiareadetail){
		if(!StringUtils.isEmpty(logiareadetail.getDetailId()) && logiareadetail.getDetailId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getLaId()) && logiareadetail.getLaId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getLaCode()) && logiareadetail.getLaCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getPointId()) && logiareadetail.getPointId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getPointCode()) && logiareadetail.getPointCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getNationCode()) && logiareadetail.getNationCode().length()>2){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{2},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getProvinceCode()) && logiareadetail.getProvinceCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getCityCode()) && logiareadetail.getCityCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getDistrictCode()) && logiareadetail.getDistrictCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiareadetail.getDisplay()) && logiareadetail.getDisplay().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logiareadetail.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
